/*************************************************
Author: tony
Date: 12/10/2012
Description:
Notes:
*************************************************/

#ifndef __CClanMgr_h__
#define __CClanMgr_h__

#include "CTArray.h"
#include "MsgComm.pb.h"
#include "commclass.h"
#include "CTHashObjectMgr.h"
#include "CClanEntry.h"
#include "CClanNameHash.h"
#include "CClanConfig.h"
#include "../gift/CGiftMgr.h"

enum EN_CLAN_MEMBER_TYPE
{
    CLAN_MEMBER_NONE   = 0,
    CLAN_MEMBER_LEADER = 1,  //族长
    CLAN_MEMBER_VICE   = 2,  //副族长
    CLAN_MEMBER_ELITE  = 3,  //精英
    CLAN_MEMBER_NORM   = 4,  //普通成员
    CLAN_MEMBER_TYPEMAX  = 5,
};

enum EN_CLAN_OPER_TYPE
{
    CLAN_OPER_INVALID    = 0,  //无效操作
    CLAN_REQ_CREATE_CLAN = 1,  //创建家族
    CLAN_REQ_DELETE_CLAN = 2,  //解散家族
    CLAN_REQ_JOIN_CLAN   = 3,  //申请加入家族
    CLAN_CANCEL_JOIN_REQ = 4,  //取消申请
    CLAN_JOIN_REQ_ACCEPT = 5,  //接受申请
    CLAN_JOIN_REQ_REJECT = 6,  //拒绝申请
    CLAN_REQ_QUIT_CLAN   = 7,  //退出家族
    CLAN_FORCE_QUIT_CLAN = 8,  //踢出家族
    CLAN_TRANS_LEADER_TITLE  = 9,   //族长转让
    CLAN_LEADER_SELF_RECOMMD = 10,  //族长自荐
    CLAN_APPOINT_TITLE = 11,  //职务任命
    CLAN_UPGRADE_CLAN   = 12,  //家族升级
    CLAN_DONATE_CLAN    = 13,  //家族捐献
    CLAN_IGNORE_ALL_REQ = 14,  //忽略所有申请
    CLAN_CHANGE_NOTICE  = 15, //修改家族公告
    CLAN_CANCEL_DELETE  = 16, //取消家族解散
    CLAN_LEARN_SKILL    = 17, //学习家族技能
    CLAN_ALLOT_BONUS    = 18, //分配家族奖励
    CLAN_SEND_RECRUIT    = 19, //家族招募
};

enum EN_CLAN_INFO_TYPE
{
    CLAN_INFO_NONE   = 0,     // 无效类型
    CLAN_INFO_MEMBER = 1,     // 成员列表
    CLAN_INFO_MEMBERLOG = 2,     //成员动态
    CLAN_INFO_ACTIVITY = 3,   // 家族活动
    CLAN_INFO_SKILL   = 4,    // 技能列表
    CLAN_INFO_PENDING = 5,    // 待审核请求
    CLAN_INFO_BASE    = 6,    // 家族基本信息
    CLAN_INFO_CLANLOG = 7,     //家族事件
    CLAN_INFO_ENYUAN = 8,     //家族恩怨
    CLAN_INFO_STORAGE = 9,     //家族仓库
};

class MsgClanBaseInfo;
class MsgClanDetailInfo;
class MsgClanSkillInfo;
class MsgClanMemberInfo;
class MsgAnsUserClanInfo;
class CPlayerSnsDetailInfo;
class CCmdDeleteClan;

class CClanMgr
{
public:

    friend class CClanTreeMgr;
    friend class CCmdDeleteClan;

    typedef CTHashObjectMgr<unsigned int, CClanEntry>   CLAN_ENTRY_HASH;
    typedef CTArray<StorageItemData, MAX_CLAN_BONUS_COUNT> T_CLAN_BONUS_ITEM;
    CClanMgr()
    {
        _iLastRankTime = 0;
        _astClanRank.Clear();
    }

    int Init(const char * sBuff);
    int CountSize();

    int InitData ();
    int InitClanData(CClanEntry & stClanEntry, CClanData & stClanData);
    int GetClanData(CClanEntry & stClanEntry, CClanData & stClanData);
    int InitUserData(CPlayerDetailInfo& stPlayerDetailInfo, CPlayerSnsDetailInfo & stPlayerSnsDetailInfo);
    int GetUserData(CUserClan & stUserClanData);

    int LoadConfig(const char* szCfgPath);

    //获取单区已有家族数
    int GetZoneClanNum ();

    unsigned int GetNewClanId();

    int GetClanConfig (unsigned int uiClanId, CClanBuildConfig & stClanBuild);

    //检查新家族名称是否存在\合法
    int CheckClanName (char * pcName, int iLen);

    //创建家族
    int CreateClan( CPlayerBaseInfo & stPlayerBaseInfo, CUserClan & stUserClan, char * pcName, int iLen);

    //解散家族
    int DeleteClan (unsigned int uiGid, CUserClan & stUserClan, unsigned int uiClanId);

    //取消解散家族
    int CancelDelete (unsigned int uiGid, CUserClan & stUserClan, unsigned int uiClanId);

    //族长转让家族
    int TransferClan (unsigned int uiGid, unsigned int uiClanId, unsigned int uiOperGId);

    //家族升级
    int UpgradeClan (unsigned int uiGid, CUserClan & stUserClan);

    //申请加入家族
    int RequestJoinClan (CPlayerBaseInfo & stPlayerBaseInfo, unsigned int uiClanId, CUserClan & stUserClan);

    //取消加入家族申请
    int CancelJoinRequest (unsigned int uiReqGid, CUserClan & stReqUserClan, unsigned int uiClanId);

    //检查是否有操作权限
    int CheckPermission (unsigned int uiGid, CClanEntry & stClanEntry, unsigned char ucOperType);

    //处理加入家族请求.
    int HandleJoinRequest(unsigned int uiHandlerGid, unsigned int uiClanId, CUserClan & stReqUserClan,
        unsigned int uiReqGid, unsigned char ucOperType);

    //退出家族
    int QuitClan (unsigned int uiReqGid, CUserClan & stReqUserClan);

    //踢出家族
    int ForceQuitClan (unsigned int uiReqGid, CUserClan & stMemberUserClan, unsigned int uiMemberGId);

    //检查并处理自荐功能
    int CheckSelfCommend (CClanEntry & stClanEntry);

    //检查
    void TimeTick (int iNow);

    //职位任命
    int AppointTitle (unsigned int uiHandlerGid, CUserClan & stHandlerClan, unsigned int uiOperGid, unsigned char ucNewTitle);

    //获取族长GID
    int GetClanLeader (unsigned int uiClanId, unsigned int & uiLeadGid);

    //获取家族
    int GetClanEntry (CUserClan & stUserClan, CClanEntry & stClanEntry);
    int GetClanEntry (unsigned int uiClanId, CClanEntry & stClanEntry);
    int GetClanIndexByClanId (unsigned int uiClanId);

    //家族捐献
    int Donate (CPlayerBaseInfo & stPlayerBaseInfo, CPlayerDetailInfo & stPlayerDetailInfo, CPlayerSnsDetailInfo & stPlayerSnsDetailInfo, int iDonateGold, int iDonateSilver);

    //修改家族公告
    int ChangeNotice (unsigned int uiReqGid, unsigned int uiClanId, char * pcNotice, int iLen);

    //学习家族技能
    int LearnSkill (unsigned char ucSkillId, CUserClan & stUserClan, int & iCurContri, unsigned int uiGid);

    //获取家族技能的二级属性
    int GetSkillAttr(CUserClan & stUserClan, CPlayerAttr & stPlayerAttr);

	
	int GetClanSkillInfo(CUserClan & stUserClan, MsgClanSkillInfo & stSkillInfo);

    int GetSkillStatusAttr(CUserClan & stUserClan, CUserClan::T_CLAN_SKILL & astClanSkill);

    //获取家族技能学习价格
    int GetLearnCost (CAttrPair & stSkill, int & iContiCost);

    //更新家族商店中物品
    int RefreshShop (unsigned int uiClanId);

    //家族排序
    int CheckAndSortClan ();
    int SortClanRank ();

    //获取家族的副本奖励(输入type: ITEM_TYPE_SILVER, ITEM_TYPE_EXP.
    //                 输出stBonusData: ITEM_TYPE_CLAN_EXP,ITEM_TYPE_CLAN_SILVER)
    int GetClanInsBonus (unsigned int uiGid, unsigned char ucType, int iCount, CDropItemData & stBonusData);

    //增加今日贡献度
    int AddTodayContri (CPlayerSnsDetailInfo & stPlayerSnsDetailInfo, int iToAddValue, int iReputaion, bool bAddLog = true);

    //增加贡献度
    int AddContri (CPlayerSnsDetailInfo & stPlayerSnsDetailInfo, int iToAddValue);

    //用户在家族中的职位
    int GetClanTitle (unsigned int uiGid, int & iTitle);

    int GetClanBaseInfo (CClanEntry & stClanEntry, CUserClan & stUserClan, MsgClanBaseInfo & stAns);
    int GetClanDetailInfo (unsigned int uiClanId, MsgClanDetailInfo & stAns);
    int GetClanMemberInfo (unsigned int uiClanId, unsigned int uiGID, MsgClanMemberInfo & stAns);
    int GetClanLog (unsigned int uiClanId, unsigned char ucLogType, MsgAnsUserClanInfo & stAns);
    int GetClanSkill (CUserClan & stUserClan, MsgAnsUserClanInfo & stAns);
    int GetClanName (unsigned uiClanId, CTSizeString<CLAN_NAME_STRING_SIZE> & stClanName);

    bool CheckBonus(CUserClan & stUserClan);
    int  GetBonusItem (CUserClan & stUserClan, CDropItem & stBonusItems);
    int GetBonusSpace (CUserClan & stUserClan);

    int TakeOutItem (unsigned int uiClanId,  T_CLAN_BONUS_ITEM & stItem);

    int AddItem (unsigned int uiClanId, CDropItem & stDropItem);

    void UpdateToDB (unsigned int uiClanId);

    int AllocBonus (unsigned int uiGid, unsigned int uiClanId);

    int GetClanIndex (unsigned int uiGid);

    //玩家没加入家族,返回0, 否则返回当前家族排名
    int GetClanRank (unsigned int uiGid);

    //根据Hash索引获取家族
    CClanEntry & GetClanEntry (unsigned int uiClanIndex);
    bool IsValidIndex (unsigned int uiClanIndex);

    //判断两个人是否在同一家族中
    bool IsInSameClan (unsigned int uiGid1, unsigned int uiGid2);

    //设置家族成员压轴竞拍获得的额外奖励
    int SetAuctionBuff (unsigned int uiGid, int iEndTime);

    int GetClanInfo (int iPageBeg, int iPageEnd, int iPageSize, CUserClan & stUserClan, ::google::protobuf::RepeatedPtrField< ::MsgClanBaseInfo > & stAns, int & iTotalPages);

    int CheckString (char* szSrc, int iLen);

    int GetClanLevel (unsigned int uiClanId);

    const char * GetUserClanName (unsigned int uiGid);

    void SetSnsClanId (unsigned int uiGid, unsigned int uiClanId);

    bool CanDoDailOpt (CPlayerBaseInfo & stPlayerBaseInfo, CPlayerDetailInfo & stPlayerDetailInfo, int iNowTime);

    int GetLeftDailCount (char chVipLevel, CPlayerDetailInfo & stPlayerDetailInfo, int iNowTime, int & iMaxDailCnt);

    //家族招募
    int Recruit (CPlayerBaseInfo & stPlayerBaseInfo, CUserClan & stReqUserClan);
public:
    CClanNameHash _stClanNameHash;
    CLAN_ENTRY_HASH _stClanEntryHash;
    int _iLastRankTime;
    CClanConfigData _stClanConfig;
    CTArray<unsigned short, MAX_CLAN_COUNT_PER_ZONE + 1> _astClanRank;   //下标为i的元素,存放排名为第(i+1)名的帮派索引

private:
    int DeleteClanInDB (CClanEntry & stClanEntry);
    void InitClanSkill (CUserClan & stUserClan);
    void NotifyClanDeleted (unsigned int uiGid);
    int SelfRecommend (CClanEntry & stClanEntry);
    void AddClanLog (CClanEntry & stClanEntry, CClanLog & stLog);
    void AddClanLog (unsigned int uiClanId, CClanLog & stLog);

    int AddToClan (unsigned int uiGid, CUserClan & stUserClan, CClanEntry & stClanEntry, EN_CLAN_MEMBER_TYPE enTitle);
    int DelFromClan (unsigned int uiGid, CUserClan & stUserClan, CClanEntry & stClanEntry);

    void DelAllJoinRequest (unsigned int uiGid, CUserClan & stUserClan);

    int GetClanLog (CClanEntry & stClanEntry, unsigned char ucLogType, MsgAnsUserClanInfo & stAns);

    int ChooseNewLeader (CClanEntry & stClanEntry);
};

#endif // __CClanMgr_h__
